<?php

namespace app\common\model;

use app\admin\controller\Loc;
use think\Exception;
use think\Model;

/**
 * 邮箱验证码
 */
class RebateLog extends Model
{
    protected $autoWriteTimestamp = true;

    protected $table = 'yzn_rebate_log';
    protected $createTime = 'create_time';


    public function user()
    {
        return $this->belongsTo(MemberModel::class,'uid', 'id')->setEagerlyType(0)->field('id');
    }


    public function user1()
    {
        return $this->belongsTo(MemberModel::class,'uid', 'id')->field('id,nickName');
    }


    public function videoUser()
    {
        return $this->belongsTo(MemberModel::class,'video_uid', 'id')->field('id,nickName');
    }


    public function alipay()
    {
        return $this->belongsTo(AliPay::class,'uid', 'uid')->setEagerlyType(0)->field('id');
    }


    public function loc()
    {
        return $this->belongsTo(FishLoc::class,'loc_id', 'id')->field('loc_name,id');
    }


    public function addLog($params = array(),$video_user)
    {
        $date = date('Y-m-d');
        $log = (new self());
        $log->day = $date;
        $log->uid = $params['uid'];
        $log->video_user_name = $video_user;
        $log->create_time = time();
        $log->loc_id = $params['loc_id'];
        $log->loc_name = $params['loc_name'];
        if (!$log->save()) {
            return false;
        }
        return true;
    }


    public function getList($param){
        $map['uid'] = $param['uid'];
        $page = $param['page'] ?? 1;
        $pagesize = (int)$param['pagesize'] ?? 10;
        $result = self::where($map)
            ->field('day, SUM(money) as total_money,status')
            ->group('day')
            ->order('create_time desc')
            ->paginate($pagesize, false, ['page' => $page]);
        return ['code' => 200, 'msg' => '获取成功', 'data' => $result];
    }


    public function getYesterdayLog($param){
        $map['uid'] = $param['uid'];
        $map['day'] = date("Y-m-d", strtotime("-1 day"));
        $page = $param['page'] ?? 1;
        $pagesize = (int)$param['pagesize'] ?? 10;
        $result = self::where($map)
            ->with('loc')
            ->field('day,status,money,loc_id')
            ->order('create_time desc')
            ->paginate($pagesize, false, ['page' => $page]);
        return ['code' => 200, 'msg' => '获取成功', 'data' => $result];
    }

}
